package com.lineage.server.timecontroller.server;

import com.lineage.config.ConfigAlt;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1GroundInventory;
import com.lineage.server.model.L1HouseLocation;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServerDeleteItemTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(ServerDeleteItemTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  private static final ArrayList<L1ItemInstance> _itemList = new ArrayList<>();
  
  public void start() {
    int timeMillis = ConfigAlt.ALT_ITEM_DELETION_TIME * 60 * 1000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, timeMillis, timeMillis);
  }
  
  public static void add(L1ItemInstance item) {
    _itemList.add(item);
  }
  
  public static boolean contains(L1ItemInstance item) {
    return _itemList.contains(item);
  }
  
  public static void remove(L1ItemInstance item) {
    if (!_itemList.remove(item))
      _log.error("地面物件删除失败 OBJID:" + item.getId()); 
    int x = item.getX();
    int y = item.getY();
    short m = item.getMapId();
    L1GroundInventory l1GroundInventory = World.get().getInventory(x, y, m);
    if (l1GroundInventory.getItem(item.getId()) != null)
      l1GroundInventory.removeItem(item); 
  }
  
  public void run() {
    try {
      if (_itemList.isEmpty())
        return; 
      World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage("\\fU10秒后将清除地上垃圾。"));
      Thread.sleep(10000L);
      World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage("\\fU完成清理地上多余的垃圾。"));
      Object[] array;
      int length = (array = _itemList.toArray()).length;
      int i = 0;
      while (i < length) {
        Object object = array[i];
        L1ItemInstance e = (L1ItemInstance)object;
        if (e != null && 
          checkItem(e))
          remove(e); 
        i++;
      } 
    } catch (Exception e2) {
      _log.error("自动清除地面物件时间轴异常重启", e2);
      GeneralThreadPool.get().cancel(this._timer, false);
      ServerDeleteItemTimer deleteItemTimer = new ServerDeleteItemTimer();
      deleteItemTimer.start();
    } 
  }
  
  private static boolean checkItem(L1ItemInstance item) {
    List<L1PcInstance> players = World.get().getVisiblePlayer((L1Object)item);
    if (item.getItem().getItemId() == 40515)
      return false; 
    if (L1HouseLocation.isInHouse(item.getX(), item.getY(), item.getMapId()))
      return false; 
    int x = item.getX();
    int y = item.getY();
    short m = item.getMapId();
    L1GroundInventory l1GroundInventory = World.get().getInventory(x, y, m);
    if (l1GroundInventory.getItem(item.getId()) == null) {
      remove(item);
      return false;
    } 
    return true;
  }
}
